// pages/user/index/index.js
var http = require('../../../utils/http.js');
Page({
  //获取用户列表
  getUserList() {
    let that = this;
    let list = this.data.list;
    wx.showLoading({
      title: 'loading',
    })
    http.GET({
      url: 'api/users/usersList',
      data: that.data.form,
      success: function(res) {
        wx.hideLoading();
        if (res.data.error === 0) {
          let newList = res.data.data.data;
          that.setData({
            'totalPage': res.data.data.last_page
          });
          for (let index = 0; index < newList.length; index++) {
            newList[index].operationClass = '';
            list.push(newList[index]);
          }
          that.setData({
            'list': list
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  //检测用户输入的关键子并进行查找
  watchInput(e){
    this.setData({
      'form.searchInfo': e.detail.value
    });
    this.setData({
      'form.page': 1
    });
    this.setData({
      'list': []
    });
    this.getUserList();
  },
  /**
   * 页面的初始数据
   */
  data: {
    operationClass: null,
    form: {
      page: 1,
      size: 7,
      searchInfo: ''
    },
    list: [],
    totalPage: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getUserList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.form.page < this.data.totalPage) {
      let page = ++this.data.form.page;
      this.setData({
        "form.page": page
      });
      this.getUserList();
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /***
   * 当用户输入文字进行搜索时
   */
  search: function(e) {
    console.log('------search---------')
  },
  /***
   * 修改某一个用户的状态停用或者开启
   */
  changeState: function(e) {
    let that = this;
    let item = e.currentTarget.dataset.item;
    let data = {};
    let content = '是否停用此用户？';
    if (item.user_state===1) {
      data = {
        id: item.id,
        user_state: 0
      }
      content = '是否开启此用户？';
    } else {
      data = {
        id: item.id,
        user_state: 1
      }
    }
    wx.showModal({
      title: '提示',
      content: content,
      success: function(res) {
        if (res.confirm) {
          http.POST({
            url: 'api/users/userState',
            data: data,
            success: function(res){
              if(res.data.error===0){
                
                that.setData({
                  "form.page": 1
                });
                that.setData({
                  "list": []
                })
                that.onLoad();
                wx.showToast({
                  title: '修改用户状态成功',
                  icon: 'none'
                });
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none'
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 去个人信息页面
   */
  toUser: function(e) {
    let item = e.currentTarget.dataset.item;
    wx.setStorage({
      key: 'item',
      data: item,
    })
    wx.navigateTo({
      url: '../user/user'
    })
  },
  /**
   * 去充值页面
   */
  toRecharge: function(e) {
    wx.navigateTo({
      url: '../recharge/recharge'
    })
  },
  /**
   * 去修改密码页面
   */
  toPassowrd: function(e) {
    wx.navigateTo({
      url: '../password/password'
    })
  },
  /***
   * 显示操作面板
   */
  showOperation: function(e) {
    let list = this.data.list;
    let that = this;
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    wx.setStorage({
      key: 'item',
      data: JSON.stringify(item),
    })
    list = list.map(function(ele,idx){
      if(idx===index){
        ele.operationClass = 'show'
      } else {
        ele.operationClass = '';
      }
      return ele;
    });
    this.setData({
      "list": list
    })
  },
  /***
   * 关闭操作面板
   */
  hideOperation: function(e) {
    let list = this.data.list;
    let that = this;
    list = list.map(function (ele, idx) {
      ele.operationClass = ''
      return ele;
    });
    this.setData({
      "list": list
    })
  },
})